﻿using BLL.Dal;
using BLL.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SysAdmin_Role_List : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["paramType"] != null && Request["paramType"] != "")
        {
            switch (Request["paramType"])
            {
                case "QueryList": QueryList(); break;
                case "AddModel": AddModel(); break;
                case "DelModel": DelModel(); break;
                case "QueryModel": QueryModel(); break;
                case "UpdateModel": UpdateModel(); break;
                default: break;
            }
        }
    }

    /// <summary>
    /// 获取所有的分页展示数据
    /// </summary>
    void QueryList()
    {

        string strWhere = "";

        int PageCount = 0;

        if (Request["page"] != null)
        {
            int.TryParse(Request["page"], out PageCount);
        }

        int RowsCount = 30;

        if (Request["rows"] != null)
        {
            int.TryParse(Request["rows"], out RowsCount);
        }

        RoleDAL roleBus = new RoleDAL();
        int recordCount = roleBus.GetRecordCount("");

        DataTable dtData = roleBus.GetListByPage("", "", (RowsCount * (PageCount - 1)), RowsCount * PageCount).Tables[0];

        object obj = new { total = recordCount, rows = dtData };

        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);

        WebHelper.HttpResponseJson(response);
    }


    /// <summary>
    /// 增加新的项
    /// </summary>
    void AddModel()
    {
        RoleDAL modelBus = new RoleDAL();
        Role model = new Role();
        model.roleCode = Guid.NewGuid().ToString();
        model.roleName = ReqHelper.Get<string>("roleName");
        model.powerCodes = ReqHelper.Get<string>("role_add_combotree");
        model.powerNames = "";

        object obj = new { success = true, msg = "数据操作成功" };
        try
        {
            if (modelBus.Add(model))
            {
                //添加角色权限表
                RolePowerDAL rolePowerBus = new RolePowerDAL();
                if (model.powerCodes != "")
                {
                    string[] strPower = model.powerCodes.Split(',');
                    foreach (string tempPower in strPower)
                    {
                        RolePower tempRolePower = new RolePower();
                        tempRolePower.ID = Guid.NewGuid();
                        tempRolePower.roleCode = model.roleCode;
                        tempRolePower.powerCode = tempPower;
                        rolePowerBus.Add(tempRolePower);
                    }
                }

            }
            obj = new { success = true, msg = "数据操作成功" };
        }
        catch (Exception error)
        {
            obj = new { success = false, msg = error.Message };
        }
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);
    }


    /// <summary>
    /// 删除选择的项
    /// </summary>
    void DelModel()
    {
        string strCode = ReqHelper.Get<string>("roleCode");
        RoleDAL roleBus = new RoleDAL();
        object obj = new { success = roleBus.Delete(strCode) };
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);
    }

    /// <summary>
    /// 查询显示项
    /// </summary>
    void QueryModel()
    {
        //string strCode = ReqHelper.Get<string>("menuCode");
        //SysMenuDAL sysMenuDal = new SysMenuDAL();
        //SysMenu sysMenu = sysMenuDal.GetModel(strCode);

        //object obj = new { success = true, message = sysMenu };
        //string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        //WebHelper.HttpResponseJson(response);
    }

    /// <summary>
    /// 更新选择的项
    /// </summary>
    void UpdateModel()
    {

        string strCode = ReqHelper.Get<string>("role_key");
        RoleDAL modelBus = new RoleDAL();
        Role model = modelBus.GetModel(strCode);
        model.roleName = ReqHelper.Get<string>("roleName");
        model.powerCodes = ReqHelper.Get<string>("role_edit_combotree");
        model.powerNames = "";

        object obj = new { success = true, msg = "数据操作成功" };
        try
        {
            if (modelBus.Update(model))
            {

                //添加角色权限表
                RolePowerDAL rolePowerBus = new RolePowerDAL();
                //删除原有权限表中的数据
                rolePowerBus.DeleteListByRoleCode(model.roleCode);

                if (model.powerCodes != "")
                {
                    string[] strPower = model.powerCodes.Split(',');
                    foreach (string tempPower in strPower)
                    {
                        RolePower tempRolePower = new RolePower();
                        tempRolePower.ID = Guid.NewGuid();
                        tempRolePower.roleCode = model.roleCode;
                        tempRolePower.powerCode = tempPower;
                        rolePowerBus.Add(tempRolePower);
                    }
                }

            }
            obj = new { success = true, msg = "数据操作成功" };
        }
        catch (Exception error)
        {
            obj = new { success = false, msg = error.Message };
        }
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);

    }
}